package ModelLayer;

import java.util.*;
/**
 * Write a description of class PersonContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PersonContainer
{
    private ArrayList<Person> personList;
    private String result;
    private String addResult;
    private static PersonContainer instance;
    private PersonContainer()
    {
        personList = new ArrayList<Person>();
    }
    
    public static PersonContainer getInstance()
    {
        if (instance == null)
        {
            instance= new PersonContainer();
        }
        return instance;
    }
    
    public ArrayList<Person> getPersonList()
    {
        return personList;
    }
    
    public void addPersons(Person person)
    {
        int index = 0;
        boolean found = false;
        while(index < personList.size() && !found)
        {
            if(person.getId().equals(personList.get(index).getId()))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(found == true)
        {
            addResult = "There is a person with that ID.";
        }
        else
        {
            personList.add(person);
            addResult = personList.get(index).getName() + " was added to friends.";
        }
    }
    
    public String addResult()
    {
        return addResult;
    }
    
    public void deletePersons(String id)
    {
        int index = 0;
        boolean found = false;
        while(index < personList.size() && !found)
        {
            if(personList.get(index).getId().equals(id))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(found == true)
        {
            result =  personList.get(index).getName() + " was deleted.";
            personList.remove(index);
        }
        else
        {
            result = "There is no person with that id.";
        }
    }
    
    public String deleteResult()
    {
        return result;
    }
    
    public void updatePersons(String newName, String newAddress, String newPostalCode, String newCity, String newPhone, String oldID)
    {
        int index =0;
        while (index <personList.size()){
            if(personList.get(index).getId().equals(oldID)){
                if(newName==""){
                }
                else {personList.get(index).setName(newName); }
                if(newAddress==""){
                }
                else { personList.get(index).setAddress(newAddress);}
                if(newPostalCode==""){
                  
                }
                else {personList.get(index).setPostalCode(newPostalCode);}
                if(newCity==""){
                  
                }
                else {personList.get(index).setCity(newCity);}
                if(newPhone==""){
                }
                else { personList.get(index).setPhone(newPhone);}
            }
            index++;
        }
    }
    
    public String showPersons(String id)
    {
        String name ="";
        int index = 0;
        boolean found = false;
        while(index < personList.size() && !found)
        {
            if(personList.get(index).getId().equals(id))
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        if(found == true)
        {
            name = "Name: " + personList.get(index).getName() + "\nAddress: " 
            + personList.get(index).getAddress() + "\nPostal code: " 
            + personList.get(index).getPostalCode() + "\nCity: "
            + personList.get(index).getCity() + "\nPhone: "
            + personList.get(index).getPhone();
        }
        else
        {
            name = "No person with that id.";
        }
        return name;
    }
}